window.onload = function (){
    // $.get({
    //     url:"findHouse",
    //     type:"get",
    //     dataType:"json",
    //     success:function (data){
    //         let result = "";
    //         for (let tr of data){
    //             result = "<TR>"+
    //                 "<TD class=house-thumb>"+
    //                 "<SPAN>"+
    //                 "<A href='details.htm' target='_blank'>"+
    //                 "<img src='../images/thumb_house.gif' width='100' height='75' alt=''>"+
    //                 "</A>"+
    //                 "</SPAN>"+
    //                 "</TD>"+
    //                 "<TD>"+
    //                 "<DL>"+
    //                 "<DT><A href='' onclick='tz("+tr.hid+")' target='_blank'><span>"+tr.title+"</span></A></DT>"+
    //                 "<DD>"+tr.townModel.townname+tr.streetModel.sname+","+tr.area+"平米<BR>联系方式："+tr.phone+"</DD>"+
    //                 "</DL>"+
    //                 "</TD>"+
    //                     "<TD class=house-type>"+tr.houseTypeModel.tname+"</TD>"+
    //                     "<TD class=house-price>"+
    //                         "<SPAN>"+tr.price+"</SPAN>元/月"+
    //                     "</TD>"+
    //                 "</TR>";
    //             $("#father").append(result);
    //         }
    //     }
    // })
    to_page(1);
    $.get({
        url: "alltype",
        type: "get",
        dataType:"json",
        success:function (data){
            let result = "";
            for (let tr of data){
                result = "<option value='"+tr.tid+"'>"+tr.tname+"</option>";
                $("#type").append(result);
            }
        }
    })
    $.get({
        url:"allstreet",
        type:"get",
        dataType:"json",
        success:function (data){
            let result = "";
            for (let tr of data){
                result = "<option value='"+tr.sid+"'>"+tr.sname+"</option>";
                $("#street").append(result);
            }
        }
    })
}

function tz(id){
    sessionStorage.setItem("houseid",id);
    location.href = "details.htm";
}

$("#search").click(function (){
    var hstreet = $("#street option:selected").val();
    var hprice = $("#price option:selected").val();
    var harea = $("#area option:selected").val();
    var htype = $("#type option:selected").val();
    var htitle = $("#htitle").val();
    if (htitle == ""){
        alert("输入框不能为空");
        return;
    }
    $.get({
        url:"likeHouse",
        type:"get",
        data:{
            title:htitle,
            price:hprice,
            type:htype,
            area:harea,
            street:hstreet
        },
        dataType: "json",
        success:function (data){
            let div = "<div></div>";
            $("#father").html(div);

            let result = "";
            for (let tr of data) {
                result = "<TR>" +
                    "<TD class=house-thumb>" +
                    "<SPAN>" +
                    "<A href='details.htm' target='_blank'>" +
                    "<img src='../images/thumb_house.gif' width='100' height='75' alt=''>" +
                    "</A>" +
                    "</SPAN>" +
                    "</TD>" +
                    "<TD>" +
                    "<DL>" +
                    "<DT><A href='' onclick='tz(" + tr.hid + ")' target='_blank'><span>" + tr.title + "</span></A></DT>" +
                    "<DD>"+tr.townModel.townname+tr.streetModel.sname+","+tr.area+"平米<BR>联系方式：" + tr.phone + "</DD>" +
                    "</DL>" +
                    "</TD>" +
                    "<TD class=house-type>" +tr.houseTypeModel.tname+"</TD>" +
                    "<TD class=house-price>" +
                    "<SPAN>" + tr.price + "</SPAN>元/月" +
                    "</TD>" +
                    "</TR>";
                $("#father").append(result);
            }
        }
    })
})


function to_page(pageNum) {
    $.ajax({
        url:"findHouse",
        data:{"startPage":pageNum},
        dataType:"json",
        success:function (data){
            showPage(data.pageNum,data.totalPage);//显示页数
            showAll(data);//显示数据
        }
    })
}

function showAll(data){
    $("#father").empty();
    $("#page").empty();
    let result = "";
    let resutl2 = "";
    for (var tr of data.data){
        result = "<TR>"+
            "<TD class=house-thumb>"+
            "<SPAN>"+
            "<A href='details.htm' target='_blank'>"+
            "<img src='../images/thumb_house.gif' width='100' height='75' alt=''>"+
            "</A>"+
            "</SPAN>"+
            "</TD>"+
            "<TD>"+
            "<DL>"+
            "<DT><A href='' onclick='tz("+tr.hid+")' target='_blank'><span>"+tr.title+"</span></A></DT>"+
            "<DD>"+tr.townModel.townname+tr.streetModel.sname+","+tr.area+"平米<BR>联系方式："+tr.phone+"</DD>"+
            "</DL>"+
            "</TD>"+
            "<TD class=house-type>"+tr.houseTypeModel.tname+"</TD>"+
            "<TD class=house-price>"+
            "<SPAN>"+tr.price+"</SPAN>元/月"+
            "</TD>"+
            "</TR>";
        $("#father").append(result);
    }
    resutl2 = "<LI class=current>"+
        "<A href='#' id='first' onclick='first()'>首页</A>"+
        "</LI>"+
        "<LI>"+
        "<A href='#' id='prev' onclick='prev("+data.pageNum+")'>上一页</A>"+
        "</LI>"+
        "<LI>"+
        "<A href='#' id='next' onclick='next("+data.pageNum+")' >下一页</A>"+
        "</LI>"+
        "<LI>"+
        "<A href='#' id='last' onclick='last("+data.totalPage+")' >末页</A>"+
        "</LI>";
    $("#page").append(resutl2);
}

function showPage(pageNum,totalPage){
    $("#pageNum").empty();
    $("#totalPage").empty();
    $("#pageNum").html(pageNum);
    $("#totalPage").html(totalPage);
}

function prev(pageNum){
    to_page(pageNum-1);
}
function next(pageNum){
    to_page(pageNum+1);
}
function first(){
    to_page(1);
}
function last(totalpage){
    to_page(totalpage);
}